package cn.edu.scau.info.hhb.jReflashDns;

import java.util.List;

public class jReflashDns {

	public static void main(String[] args) {
		engine();
	}

	public static void engine() {
		List<Domain> lst = Loader.load();
		List<String> lstIP = IPUtil.getAllIP();
		for (Domain domain : lst) {
			String ip = null;
			for (String strIP : lstIP) {
				if (IPUtil.match(strIP, domain.getIpMatch(),
						domain.getIpDismatch())) {
					ip = strIP;
					break;
				}
			}
			if (ip == null) {
				System.err.println("IP regx Err: " + domain);
				continue;
			}

			switch(domain.getType()){
			case Domain.TYPE_PUBLIC:
				String html =IPUtil.resolveHtmlIP(ip);
				if(html==null){
					System.out.println("ResolveIP Error.");
					continue;
				}
				String publicIP= IPUtil.parseIP(html);
				if(publicIP==null){
					System.out.println("ResolveIP Error.");
					continue;
				}
				ip=publicIP;
				
			case Domain.TYPE_LOCAL:
				DDNSoray oray = new DDNSoray(ip,domain.getOaryUsr(),domain.getOrayPasswd(),domain.getOrayDomain());
				System.out.println("Result :\n"+domain);
				System.out.println("IP: "+ip);
				System.out.println(oray.update());
				break;
			}
		}
	}

}
